package com.pn.service.impl;

import com.pn.entity.Purchase;
import com.pn.entity.Result;
import com.pn.mapper.PurchaseMapper;
import com.pn.page.Page;
import com.pn.service.PurchaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class PurchaseServiceImpl implements PurchaseService {
    @Autowired
    private PurchaseMapper purchaseMapper;

    /**
     * 添加采购单的业务方法
     *
     * @param purchase 采购单表buy_list表的实体类
     * @return 添加结果类
     */
    @Override
    public Result savePurchase(Purchase purchase) {
        int i = purchaseMapper.insertPurchase(purchase);
        if (i > 0) {
            return Result.ok("采购单添加成功");
        }
        return Result.err(Result.CODE_ERR_BUSINESS, "采购单添加失败");
    }

    @Override
    public Page queryPurchasePage(Page page, Purchase purchase) {
        //查询采购单总行数
        int purchaseCount = purchaseMapper.selectPurchaseCount(purchase);
        //分页查询采购单
        List<Purchase> purchaseList = purchaseMapper.selectPurchasePage(page,
                purchase);
//将查询到的总行数和当前页数据组装到 Page 对象
        page.setTotalNum(purchaseCount);
        page.setResultList(purchaseList);
        return page;
    }
}
